package com.originskyseed.platform.count.controller;

import java.util.Arrays;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.originskyseed.platform.core.aspect.annotation.RequiredParams;
import com.originskyseed.platform.core.base.BaseController;
import com.originskyseed.platform.core.base.BaseLocaleResource;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.base.BaseResponseBody;
import com.originskyseed.platform.core.common.code.CommonResponseCode;
import com.originskyseed.platform.core.filter.helper.SingleSignOnTokenHelper;
import com.originskyseed.platform.count.i18n.CountResponseMsg;
import com.originskyseed.platform.count.model.FeeCategoryRelUser;
import com.originskyseed.platform.count.service.IFeeCategoryRelUserService;
import com.originskyseed.platform.count.vo.request.FeeCategoryRelUserRequestVO;
import com.originskyseed.platform.user.model.UserInfo;

/**
 * 费用类别关联用户表Controller类（OSI_FEE_CATEGORY_REL_USER）
 *
 * @author wlj
 */
@Controller
@RequestMapping("/feeCategoryRelUser")
@BaseLocaleResource(localeFileName = "i18n_count")
public class FeeCategoryRelUserController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(FeeCategoryRelUserController.class);

	@Resource
	private IFeeCategoryRelUserService feeCategoryRelUserService;

	/**
	 * 分页查询
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategoryRelUserList.json", method = RequestMethod.POST)
	@RequiredParams(names = { "pageNo", "pageSize" })
	public BaseResponseBody<BasePage<FeeCategoryRelUser>> feeCategoryRelUserList(HttpServletRequest request) {
		BaseResponseBody<BasePage<FeeCategoryRelUser>> result = null;
		try {
			// 请求对象
			FeeCategoryRelUserRequestVO requestVO = this.getParameterBean(FeeCategoryRelUserRequestVO.class);

			// 分页查询
			BasePage<FeeCategoryRelUser> page = new BasePage<FeeCategoryRelUser>(requestVO.getPageNo(), requestVO.getPageSize());
			page = this.feeCategoryRelUserService.selectListByPage(requestVO.toMap(), page);
			result = new BaseResponseBody<BasePage<FeeCategoryRelUser>>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_SUCCESS.getMsg()), page);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<BasePage<FeeCategoryRelUser>>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.LIST_LOAD_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 保存信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategoryRelUserSave.json", method = RequestMethod.POST)
	@RequiredParams(names = {})
	public BaseResponseBody<FeeCategoryRelUser> feeCategoryRelUserSave(HttpServletRequest request) {
		BaseResponseBody<FeeCategoryRelUser> result = null;
		try {
			// 请求对象
			FeeCategoryRelUser feeCategoryRelUser = this.getParameterBean(FeeCategoryRelUser.class);
			UserInfo userInfo = SingleSignOnTokenHelper.getCurrentUserInfo(UserInfo.class);
			if (StringUtils.isNotBlank(feeCategoryRelUser.getId())) {
				this.feeCategoryRelUserService.updateByPrimaryKeySelective(feeCategoryRelUser);
			} else {
				feeCategoryRelUser.setCreator(userInfo.getName());
				feeCategoryRelUser.setCreatorId(userInfo.getId());
				feeCategoryRelUser.setIsDel(0);
				this.feeCategoryRelUserService.insertSelective(feeCategoryRelUser);
			}
			result = new BaseResponseBody<FeeCategoryRelUser>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_SUCCESS.getMsg()), feeCategoryRelUser);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<FeeCategoryRelUser>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_SAVE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

	/**
	 * 删除信息
	 *
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/feeCategoryRelUserDelete.json", method = RequestMethod.POST)
	@RequiredParams(names = { "ids" })
	public BaseResponseBody<Boolean> feeCategoryRelUserDelete(HttpServletRequest request) {
		BaseResponseBody<Boolean> result = null;
		try {
			// 请求参数
			String[] ids = this.getParameterArray("ids", new String[] {});
			this.feeCategoryRelUserService.deleteByPrimaryKeys(Arrays.asList(ids));
			result = new BaseResponseBody<Boolean>(true, CommonResponseCode.SUCCESS.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_DELETE_SUCCESS.getMsg()), true);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			result = new BaseResponseBody<Boolean>(false, CommonResponseCode.ERROR.getCode(), this.getLocaleMessage(this.getClass(), CountResponseMsg.DATA_DELETE_ERROR.getMsg()), e.getMessage(), null);
		}
		return result;
	}

}
